/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SignalPanel.java
 *
 * Created on 09.12.2011., 16.17.52
 */
package aor2.gui.cpu.signals;

import aor2.components.Signal;
import aor2.components.records.RecordSignal;
import aor2.cpu.ClockCPU;
import java.awt.Color;
import java.awt.Graphics;

/**
 * 
 * @author Moxi
 */
public class SignalPanel extends javax.swing.JPanel {

	private int clockCycles;
	private Signal signal;

	/** Creates new form SignalPanel */
	public SignalPanel(Signal signal, int clockCycles) {
		initComponents();
		this.signal = signal;
		this.clockCycles = clockCycles;
	}

	private int x(int left, int width, int cyclesBefore) {
		return (int) (0.1 * width + left + (clockCycles - cyclesBefore) * 0.8
				* width / clockCycles);
	}

	@Override
	protected void paintComponent(Graphics g) {

		super.paintComponent(g);
		int width = this.getWidth();
		int height = this.getHeight();
		int x = this.getX();
		int y = this.getY();

		int zero = (int) (y + (0.9 * height));
		int one = (int) (y + (0.1 * height));
		// int z = (int)(y + (0.5 * height));

		// int step = (int)(width*0.8/clockCycles);

		ClockCPU clk = ClockCPU.getInstance();

		int coordY = (int) (y + (0.95 * height));
		int coordYMin = (int) (y + (0.94 * height));
		int coordYMax = (int) (y + (0.96 * height));

		g.drawLine(x, coordY, x + width, coordY);
		for (int i = 0; i < clockCycles + 1; i++) {
			int xC = x(x, width, i);
			g.drawLine(xC, coordYMax, xC, coordYMin);
		}

		int fromTop = 0;
		RecordSignal recSig = (RecordSignal) signal.getHistory().getFromTop(
				fromTop);

		int currentT = clk.getT();
		boolean currLvl = recSig.getValue();
		int tmpT = currentT;// desna strana signala
		int xL, xR;
		xR = x(x, width, 0);
		do {
			xL = Math.max(x(x, width, (currentT - recSig.getMark().getT())),
					(int) (0.1 * width));
			boolean active = recSig.getValue();
			if (currLvl != active) {
				g.drawLine(xR, one, xR, zero);
			}
			currLvl = active;
			int lvl = active ? one : zero;

			g.setColor(Color.black);
			g.drawLine(xL, lvl, xR, lvl);

			xR = xL;
			tmpT = recSig.getMark().getT();
			fromTop++;
			recSig = (RecordSignal) signal.getHistory().getFromTop(fromTop);
			if (recSig == null)
				break;
		} while (tmpT > Math.max(0, currentT - clockCycles));

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		setBackground(new java.awt.Color(227, 249, 249));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 411,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 69,
				Short.MAX_VALUE));
	}// </editor-fold>//GEN-END:initComponents
		// Variables declaration - do not modify//GEN-BEGIN:variables
		// End of variables declaration//GEN-END:variables
}
